import React from 'react'
import { InputNumber, Button, Switch, Input, Modal, message } from 'antd';
import { BaseListPage, IBaseListPageProps, IBaseListPageState, http, uaaApp, validators } from '../../../lib/index'
import { UdModal, UdModalForm } from '../../../lib/ui/UdModal';
import UdSelect from '@/components/UdFormControls/UdSelect'
import udMainStore from '@/store/UdMain'
import apis from '@/apis/AnnouncementManage'
import dayjs from 'dayjs';

interface IRepairListProps extends IBaseListPageProps { }

interface IRepairListState extends IBaseListPageState { sortValue: number }

const AddBtnAuth = 'customer_dealer_notice_class_manager_insert'
const UpdateBtnAuth = 'customer_dealer_notice_class_manager_update'
const UpdateStatusBtnAuth = 'customer_dealer_notice_class_manager_status'

//接口字段映射
const fieldMap = {
  //分类名称
  name: "noticeClassName",
  //排序
  sort: "sort",
  //状态
  status: "status",
  //所属销售组织
  salesOrganization: "brandName",
  //操作人
  operator: "createdBy",
  //更新时间
  updateTime: "updatedTime",
  //状态冻结
  statusFreeze1: 1,
  //状态启用
  statusEnable: 0
}

export default class AnnouncementManagementClassification extends BaseListPage<IRepairListProps, IRepairListState> {
  private brandList = udMainStore.brandList

  constructor(props: IRepairListProps) {
    super(props)
    //页面配置
    this.state = {
      //排序值
      sortValue: 0,

      //标题
      title: '公告分类管理',
      //表单请求地址
      queryApi: apis.getClassificationList,
      method: 'GET',
      rowKey: (record, index) => index + "",
      //表格项配置
      columns: [
        ["分类名称", fieldMap.name],
        /* {
          title: "排序", key: fieldMap.sort, render: (text, record, index) => <InputNumber
            min={1}
            defaultValue={record[fieldMap.sort]}
            onBlur={(e) => { this.handleSort(e, record) }}
          />
        }, */
        {
          title: "状态", key: fieldMap.status, render: (text, record, index) => <Switch
            checkedChildren="启用"
            unCheckedChildren="冻结"
            checked={record[fieldMap.status] === fieldMap.statusEnable}
            onClick={() => { this.handleChangeStatus(record.id, record[fieldMap.status]) }}
            disabled={!uaaApp.canUse(UpdateStatusBtnAuth)}
          />
        },
        ["所属销售组织", fieldMap.salesOrganization],
        ["操作人", fieldMap.operator],
        ["更新时间", fieldMap.updateTime,(text)=>text?dayjs(text).format('YYYY-MM-DD HH:mm:ss'):''],
      ],
      //页面操作项配置
      leftBtns: [
        uaaApp.canUse(AddBtnAuth) && <Button type="primary" onClick={() => { this.addNew() }}>新建分类</Button>
      ],
      tableProps: {
        rowSelection: null,
      }
    }
  }

  //新增分类
  private addNew = () => {
    UdModal.open({
      title: "新增分类",
      content: <UdModalForm
        items={[{
          label: '品牌公司',
          id: 'brandCode',
          rules: [validators.required],
          render: <UdSelect showSearch options={this.brandList} />,
        },{
          id: fieldMap.name,
          label: '分类名称',
          render: <Input placeholder="请输入分类名称，最长为10个字符" />,
          rules: [validators.required, validators.max(10)],
          decoratorOptions: { validateFirst: true }
        },]}
      />,
      okText: "保存",
      onOk: (value) => {
        return http.post(apis.postAddClassification, value ).then(res=>{
          message.success('新增成功')
          this.query()
        })
      }
    })
  }

  //修改状态
  private handleChangeStatus = (id, status) => {
    status = status === fieldMap.statusEnable ? fieldMap.statusFreeze1 : fieldMap.statusEnable
    Modal.confirm({
      title: "修改分类状态",
      content: status === fieldMap.statusEnable ? "即将启用该条分类，是否继续？" : "即将冻结该条分类，是否继续？",
      onOk: () => {
        return http.post(apis.postEditClassificationStatus + "?id=" + id + "&status=" + status).then(res=>{
          message.success('修改成功')
          this.query()
        })
      }
    })
  }

  //排序提交
  private handleSort = (e, record) => {
    if (e.target.value * 1 !== record[fieldMap.sort]) {
      http.post(apis.postClassificationSort, {
        id: record.id,
        sort: e.target.value * 1
      }).then(res => {
        this.query()
      }).catch(err => {
        e.target.value = record[fieldMap.sort]
      })
    }
  }

}
